package com.example.androidlearing2423.ui.theme.act.ui


import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.activity.enableEdgeToEdge
import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.example.androidlearing2423.R
import com.example.androidlearing2423.ui.theme.theme.AndroidLearing2423Theme

data class Message(
    val id: String,
    val avatar: Int,
    val name: String,
    val content: String,
    val time: String,
    val unreadCount: Int = 0,
    val isMuted: Boolean = false
)

class MainActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        setContent {
            AndroidLearing2423Theme{
                Scaffold(
                    modifier = Modifier.fillMaxSize(),
                    topBar = {
                        Box(
                            modifier = Modifier
                                .fillMaxWidth()
                                .height(56.dp)
                                .background(Color(0xFFEDEDED)),
                            contentAlignment = Alignment.Center
                        ) {
                            Text(
                                text = "微信",
                                fontSize = 20.sp,
                                fontWeight = FontWeight.Bold
                            )
                        }
                    }
                ) { innerPadding ->
                    MessageList(
                        messages = getSampleMessages(),
                        modifier = Modifier
                            .padding(innerPadding)
                            .fillMaxSize()
                    )
                }
            }
        }
    }

    private fun getSampleMessages(): List<Message> {
        return listOf(
            Message(
                id = "1",
                avatar = R.drawable.yi,
                name = "张三",
                content = "晚上一起吃饭吗？",
                time = "18:30",
                unreadCount = 2
            ),
            Message(
                id = "2",
                avatar = R.drawable.two,
                name = "李四",
                content = "这个文件你看一下",
                time = "昨天"
            ),
            Message(
                id = "3",
                avatar = R.drawable.three,
                name = "开发小组",
                content = "王五：需求今天完成",
                time = "周一",
                unreadCount = 5
            ),
            Message(
                id = "4",
                avatar = R.drawable.four,
                name = "妈妈",
                content = "记得按时吃饭",
                time = "08:15",
                unreadCount = 1
            ),
            Message(
                id = "5",
                avatar = R.drawable.five,
                name = "工作群",
                content = "下周会议取消",
                time = "周三",
                isMuted = true
            )
        )
    }
}

@Composable
fun MessageList(
    messages: List<Message>,
    modifier: Modifier = Modifier
) {
    LazyColumn(modifier = modifier) {
        items(messages) { message ->
            MessageItem(message = message)
            Box(
                modifier = Modifier
                    .fillMaxWidth()
                    .height(0.5.dp)
                    .background(Color(0xFFEEEEEE))
                    .padding(horizontal = 78.dp)
            )
        }
    }
}

@Composable
fun MessageItem(message: Message) {
    Row(
        modifier = Modifier
            .fillMaxWidth()
            .height(80.dp)
            .padding(horizontal = 16.dp),
        verticalAlignment = Alignment.CenterVertically
    ) {
        // 方形头像（圆角8dp）
        Image(
            painter = painterResource(id = message.avatar),
            contentDescription = "联系人头像",
            modifier = Modifier
                .size(50.dp)
                .clip(RoundedCornerShape(8.dp))
        )

        Spacer(modifier = Modifier.width(12.dp))

        Column(
            modifier = Modifier
                .weight(1f)
                .height(50.dp),
            verticalArrangement = Arrangement.SpaceBetween,
            horizontalAlignment = Alignment.Start
        ) {
            Row(
                modifier = Modifier.fillMaxWidth(),
                horizontalArrangement = Arrangement.SpaceBetween
            ) {
                Text(
                    text = message.name,
                    fontSize = 16.sp,
                    color = Color.Black,
                    fontWeight = FontWeight.Medium
                )
                Text(
                    text = message.time,
                    fontSize = 12.sp,
                    color = Color(0xFF999999)
                )
            }

            Text(
                text = message.content,
                fontSize = 14.sp,
                color = Color(0xFF666666),
                maxLines = 1
            )
        }
    }
}

@Preview(showBackground = true)
@Composable
fun MessageListPreview() {
    AndroidLearing2423Theme{
        MessageList(
            messages = listOf(
                Message(
                    id = "1",
                    avatar = R.drawable.yi,
                    name = "张三",
                    content = "晚上一起吃饭吗？",
                    time = "18:30",
                    unreadCount = 2
                )

            )
        )
    }
}